package com.example.demo.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * 统一的返回数据
 */
@Slf4j
@Data
public class Response<T> implements Serializable {


    /**
     * 状态码
     */
    private String code;

    /**
     * 信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;


    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    private Long total;

    public static <T> Response<T> success() {
        return success(ResultEnum.OK.getMsg());
    }

    public static <T> Response<T> success(T data) {
        return success(data, ResultEnum.OK.getMsg());
    }

    public static <T> Response<T> data(T data) {
        return success(data, ResultEnum.OK.getMsg());
    }
    public static <T> Response<T> success(String message) {
        return success(null, message);
    }

    public static <T> Response<T> success(T data, String msg) {
        Response<T> response = new Response<>();
        response.setData(data);
        response.setMsg(msg);
        response.setCode(ResultEnum.OK.getCode());
        return response;
    }

    public Response<T> setTotal(Long total) {
        this.total = total;
        return this;
    }


    public static <T> Response<T> fail(ResultEnum resultEnum) {
      return fail(resultEnum,null);
    }

    public static <T> Response<T> fail(ResultEnum resultEnum, T data) {
        return fail(resultEnum.getCode(), resultEnum.getMsg(), data);
    }

    public static <T> Response<T> fail(String code, String msg, T data) {
        Response<T> response = new Response<>();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }

}
